﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Forum.BLL.Member;
using Forum.Entity.Member;
using Frame.Lib.Web;
using Frame.Lib.Utilities;
using Frame.Lib.Common;


namespace Forum.Web.Admin.UForum.Member
{
    public partial class List : PageList
    {
        private MemberExBO ForumMember
        {
            get
            {
                return MemberExBO.Instance<MemberExBO>();
            }
        }

        private MemberBaseBO MemberBase
        {
            get
            {
                return MemberBaseBO.Instance<MemberBaseBO>();
            }
        }

        private string LoginName
        {
            get
            {
                return Request.QueryString["LoginName"];
            }
        }

        private string Name
        {
            get
            {
                return Request.QueryString["Name"];

            }
        }

        private string Mobile
        {
            get
            {
                return Request.QueryString["Mobile"];
            }
        }

        private string Email
        {
            get
            {
                return Request.QueryString["Email"];
            }
        }
      
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {               
                txtLoginName.Text = this.LoginName;
                txtName.Text = this.Name;
                txtEmail.Text = this.Email;
                txtMobile.Text = this.Mobile;

                BindView();
            }
            AddAttr();
        }

        

        private void AddAttr()
        {
            base.ShowForm(lnkAdd, "增加会员", GlobalVar.WebCurrPath + "Edit.aspx", 450, 360);
            
        }

        private void BindView()
        {
            LVUtil.Init(lv, lvHeader);
            chkSelectAll.Checked = false;
            PagerSetting setting = new PagerSetting();
            setting.PageIndex = this.PageIndex;
            setting.OrderBy = "ID desc";
            setting.Where = "1=1";
            setting.PageSize = AppSetting.PageSize;


           
            if (txtLoginName.Text.Trim() != "")
            {
                setting.Where += " and LoginName = @p0";
            }
            if (txtName.Text.Trim() != "")
            {
                setting.Where += " and Name = @p1";
            }
            if (txtMobile.Text.Trim() != "")
            {
                setting.Where += " and Mobile = @p2";
            }
            if (txtEmail.Text.Trim() != "")
            {
                setting.Where += " and Email = @p3";
            }
           // _setting.SortExpression = "RegisterTime";
            //_setting.SortDirection = Jzy.Core.Common.SortDirection.desc;
            //_setting.PageSize = 20;

            IEnumerable<MemberBaseInfo> baseList = this.MemberBase.GetList(setting, txtLoginName.Text.Trim(), txtName.Text.Trim(), txtMobile.Text.Trim(), txtEmail.Text.Trim());
            IEnumerable<MemberExInfo> list = this.ForumMember.GetList(baseList.Select(T => T.ID));
            foreach (MemberExInfo info in list)
            {
                info.MemberBaseInfo = baseList.Where(T => T.ID == info.ID).First();
            }

            lv.DataSource = list;
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataBind();

            lblStatistic.Text = "会员列表&nbsp;[&nbsp;共&nbsp;" + setting.RecordCount + "&nbsp;个&nbsp;]";

            string prefix = GlobalVar.WebCurrPath + "List.aspx?LoginName=" + txtLoginName.Text.Trim() + "&Name=" + txtName.Text.Trim() + "&Mobile=" + txtMobile.Text.Trim() + "&Email=" + txtEmail.Text.Trim();

           
            litPager.Text = HtmlPager.Render(setting, prefix, true);
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                HyperLink lnkEdit = (HyperLink)e.Item.FindControl("lnkEdit");

                string strID = StrUtil.EncryptUrl(lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString());

                JSUtil.ShowForm(lnkEdit, "修改用户信息", null, GlobalVar.WebCurrPath + "Edit.aspx?ID=" + strID, 450, 360);

            }
        }

        protected void btnSerch_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }
            this.ForumMember.Delete(strSelectIDs, true);
            BindView();
        }
    }
}